package ua.ck.example.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ua.ck.example.domain.Product;
import ua.ck.example.domain.Provider;

@Repository
public class ProviderDAOImpl implements ProviderDAO {
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void addProvider(Provider provider) {
		sessionFactory.getCurrentSession().save(provider);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Provider getProvider(Provider provider) {
		StringBuffer sql = new StringBuffer("FROM Provider p WHERE 1=1");
		sql.append(" and p.providerName='" + provider.getProviderName()+"'");
		sql.append(" and p.password='" + provider.getPassword()+"'");
		List<Provider> providerList = (List<Provider>) sessionFactory
				.getCurrentSession().createQuery(sql.toString()).list();
		if (providerList != null && providerList.size() > 0) {
			return providerList.get(0);
		}
		return null;
	}
}
